//
//  PBVerifyInfoCell.m
//  PBHomeDemo
//
//  Created by Playboy on 2018/1/12.
//  Copyright © 2018年 tmkj. All rights reserved.
//

#import "PBVerifyInfoCell.h"

@interface PBVerifyInfoCell ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate,UITextFieldDelegate>

@property (strong, nonatomic) IBOutlet UITextField *nameTextField;
@property (strong, nonatomic) IBOutlet UITextField *cardTextField;
@property (strong, nonatomic) IBOutlet UIButton *firstButton;
@property (strong, nonatomic) IBOutlet UIButton *secButton;
@property (nonatomic, strong) UIButton *selectButton;
//@property (nonatomic, strong) NSString  *firstImageUrl;
//@property (nonatomic, strong) NSString  *secImageUrl;

@end

@implementation PBVerifyInfoCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.nameTextField.delegate = self;
    self.cardTextField.delegate = self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

#pragma mark - UITextFieldDelegate
- (void)textFieldDidEndEditing:(UITextField *)textField {
    if (textField == self.nameTextField) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(backWithValue:key:)]) {
            [self.delegate backWithValue:textField.text key:@"realName"];
        }
    }else if (textField == self.cardTextField) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(backWithValue:key:)]) {
            [self.delegate backWithValue:textField.text key:@"identifyCardNum"];
        }
    }
}

- (IBAction)buttonClick:(UIButton *)sender {
    self.selectButton = sender;
    [self showAlert];
}
- (void)showAlert {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"选择类型" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self openImage:1];
    }];
    UIAlertAction *lifeAction = [UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self openImage:0];
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }];
    
    [alertController addAction:okAction];
    [alertController addAction:lifeAction];
    [alertController addAction:cancelAction];
    // 出现
    [self.jumpVC presentViewController:alertController animated:YES completion:^{
        NSLog(@"presented");
    }];
}
#pragma mark - image
- (void)openImage:(NSInteger)type
{
    UIImagePickerController *picker =[[UIImagePickerController alloc]init];
    picker.allowsEditing= YES;
    picker.delegate= self;
    picker.sourceType = type;
    [self.jumpVC presentViewController:picker animated:YES completion:nil];
    
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    NSData *imageData = UIImagePNGRepresentation(image);
    if (self.selectButton.tag == 100) {
        [TMNetwork requestImageWithImage:imageData url:@"/file/upload" callback:^(TMBaseReqModel *model) {
            [picker dismissViewControllerAnimated:YES completion:nil];
            if (model.code == 200) {
                [self.firstButton setBackgroundImage:image forState:UIControlStateNormal];
                [self.firstButton setTitle:@"" forState:UIControlStateNormal];
                if (self.delegate && [self.delegate respondsToSelector:@selector(backWithValue:key:)]) {
                    [self.delegate backWithValue:model.data key:@"identifyCardImageFront"];
                }
            }else {
                [self.jumpVC showHint:@"连接服务器失败，请稍后再试"];
            }
        }];
    }else {
        [TMNetwork requestImageWithImage:imageData url:@"/file/upload" callback:^(TMBaseReqModel *model) {
            [picker dismissViewControllerAnimated:YES completion:nil];
            if (model.code == 200) {
                [self.secButton setBackgroundImage:image forState:UIControlStateNormal];
                [self.secButton setTitle:@"" forState:UIControlStateNormal];
                if (self.delegate && [self.delegate respondsToSelector:@selector(backWithValue:key:)]) {
                    [self.delegate backWithValue:model.data key:@"identifyCardImageBack"];
                }
            }else {
                [self.jumpVC showHint:@"连接服务器失败，请稍后再试"];
            }
        }];
    }
    [picker dismissViewControllerAnimated:YES completion:nil];
}
@end
